package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.dto.YddAdvimgDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.mapper.YddAdvimgMapper;
import com.itheima.ydd.service.YddAdvimgService;
import com.itheima.ydd.service.YddAdvposService;
import com.itheima.ydd.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class YddAdvimgServiceImpl extends ServiceImpl<YddAdvimgMapper, YddAdvimg> implements YddAdvimgService {
    @Autowired
    YddAdvposService yddAdvposService;
    @Override
    public PageUtils<YddAdvimgDto> getPage(String advimgdesc, String advimgpos, Integer page, Integer limit) {
        LambdaQueryWrapper<YddAdvimg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(advimgdesc),YddAdvimg::getAdvimgdesc,advimgdesc).eq(!StringUtils.isEmpty(advimgpos),YddAdvimg::getAdvimgpos,advimgpos);
        Page iPage = new Page<>(page,limit);
        page(iPage,queryWrapper);
        Page<YddAdvimgDto> dtoPage = new Page<>();

        List<YddAdvimg> records = iPage.getRecords();

        List<YddAdvimgDto> list = records.stream().map((record)-> {
            YddAdvimgDto yddAdvimgDto = new YddAdvimgDto();
            BeanUtils.copyProperties(record, yddAdvimgDto);
            String advimgposId = record.getAdvimgpos();
            YddAdvpos yddAdvpos = yddAdvposService.getById(advimgposId);
            yddAdvimgDto.setAdvposname(yddAdvpos.getAdvposname());
            return yddAdvimgDto;
        }).collect(Collectors.toList());

        PageUtils<YddAdvimgDto> yddAdvposPage = new PageUtils<>(iPage);
        yddAdvposPage.setData(list);

        return yddAdvposPage;
    }
}
